package src.G_E_AnnotationAndReflection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 使用反射获取类信息（注解）
public class G02_ReflectionTest04_3 {
    public static void main(String[] args) throws Exception {
        Class c1 = Class.forName("src.G_E_AnnotationAndReflection.RT_04_3_User");
        // 获取类注解
        Table annotation = (Table)c1.getAnnotation(Table.class);
        // 获取value
        System.out.println(annotation.value());
        // 获取属性注解
        java.lang.reflect.Field id = c1.getDeclaredField("id");
        Field annotation1 = id.getAnnotation(Field.class);
        // 获取Columname值
        System.out.println(annotation1.Columname());
    }
}

@Table("db_student")
class RT_04_3_User {
    @Field(Columname = "db_id", type = "int", length = 10)
    private int id;
    private int age;
    private String name;

    public RT_04_3_User() {
    }

    public RT_04_3_User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

// 类名的注解
@Target(ElementType.TYPE) // 元注解，类用注解
@Retention(RetentionPolicy.RUNTIME) // 元注解，运行时有效
@interface Table {
    String value();
}

// 属性的注解
@Target(ElementType.FIELD) // 元注解，属性注解
@Retention(RetentionPolicy.RUNTIME) // 元注解，运行时有效
@interface Field {
    String Columname();

    String type();

    int length();
}

